/**
 * 
 */
package org.mspring.mlog.web.module.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Log;
import org.mspring.mlog.service.LogService;
import org.mspring.mlog.service.OptionService;
import org.mspring.mlog.web.query.LogQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Order;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Gao Youbo
 * @since 2013-3-12
 * @description
 * @TODO
 */
@Widget
@RequestMapping("/admin/log")
public class Admin_LogWidget extends Admin_AbstractWidget {

    @Autowired
    private LogService logService;
    @Autowired
    private OptionService optionService;

    @RequestMapping("/view")
    @org.mspring.mlog.web.support.log.Log(action = "查看系统日志")
    public String view(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            Object obj = getSessionAttribute(request, "LogWidget_view_id");
            if (obj != null) {
                id = (Long) obj;
            }
        }
        if (id == null) {
            return prompt(model, "请选择要查看的日志");
        }
        Log log = logService.get(id);
        if (log == null) {
            return prompt(model, "请选择要查看的日志");
        }
        model.addAttribute("log", log);
        setSessionAttribute(request, "LogWidget_view_id", id);
        return "/admin/log/viewLog";
    }

    @RequestMapping("/list")
    @org.mspring.mlog.web.support.log.Log(action = "系统日志列表")
    public String list(@ModelAttribute Page<Log> logPage, @ModelAttribute Log log, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (logPage == null) {
            logPage = new Page<Log>();
        }
        logPage.setSort(new Order("id", Order.OrderType.DESC));
        logPage = logService.listPage(new LogQueryCriterion(queryParams), logPage);
        model.addAttribute("logPage", logPage);
        model.addAllAttributes(queryParams);
        return "/admin/log/listLog";
    }

    @RequestMapping("/clear")
    @org.mspring.mlog.web.support.log.Log(action = "清理系统日志")
    public String log(@RequestParam(required = false) int days, HttpServletRequest request, HttpServletResponse response, Model model) {
        logService.deleteLogs(days);
        return prompt(model, "系统提示", "日志清理成功！", "/admin/log/list");
    }

    @RequestMapping("/config")
    public String config(@RequestParam Map<String, String> options, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "/admin/log/config";
    }

    @RequestMapping("/config/save")
    @org.mspring.mlog.web.support.log.Log(action = "配置系统日志")
    public String config_save(@RequestParam Map<String, String> options, HttpServletRequest request, HttpServletResponse response, Model model) {
        optionService.setOptions(options);
        return prompt(model, "配置成功", "/admin/log/config");
    }
}
